package com.jiaai.cloud.wx.controller;


import com.alibaba.fastjson.JSONObject;
import com.jiaai.cloud.base.entity.RestResult;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.redis.util.RedisUtil;
import com.jiaai.cloud.base.weixin.util.WeiXinUtil;
import com.jiaai.cloud.wx.entity.TWxshop;
import com.jiaai.cloud.wx.entity.TWxuser;
import com.jiaai.cloud.wx.service.ITWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 佳爱网络科技
 * @since 2017-08-22
 */
@Controller
@RequestMapping("/api/v1/twxuser")
public class TWxuserController {
    @Autowired
    private ITWxuserService itWxuserService;

    //todo tested
    /**
     * 基本信息查询
     * @param userid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/findById",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object findById(@RequestHeader("userid") Integer userid)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxuserService.selectById(userid));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 注册用户信息, 利用前端的code值,通过wx工具类得到userInfo
     * @param code
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/reg",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object reg(String code, @RequestBody TWxuser user)  throws ServiceException {
        try {
            //todo appsecret need to be changed
            Object session = WeiXinUtil.getAppSessionKey("wx2d74b3b5e1dc25ed", "0709e19de891eb2a9810470f2789621f", code).get("session");
            JSONObject jsonObject = (JSONObject) session;
            String openid = jsonObject.get("openid").toString();
            TWxuser oldUser = itWxuserService.selectUserByOpenID(openid);

            if(oldUser == null){
                user.setOpenID(openid);
                user.setRegdate(new Date());
                itWxuserService.insertBackUserid(user);
            }else{
                user=oldUser;
            }

            return RestResultGenerator.genSuccessResult(user);
        }catch (Exception e) {
            throw new ServiceException("注册失败", e.getMessage());
        }
    }

    /**
     * 删除用户信息
     * @param userid
     * @return
     * @throws ServiceException
     */
    //todo tested
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object delete(@RequestHeader("userid") Integer userid)  throws ServiceException {
        try {

            itWxuserService.deleteById(userid);
            return RestResultGenerator.genSuccessResult("删除用户成功");
        }catch (Exception e) {
            throw new ServiceException("删除失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 修改用户信息, 区别与微信的用户信息********
     * @param userid
     * @param user
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object update(@RequestHeader("userid") Integer userid, @RequestBody TWxuser user)  throws ServiceException {
        try {
            user.setUserid(userid);
            itWxuserService.updateById(user);
            return RestResultGenerator.genSuccessResult("修改用户成功");
        }catch (Exception e) {
            throw new ServiceException("修改失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 返回用户附近的商店列表,带有距离数值
     * @param longitude
     * @param latitude
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getNearShops/{longitude}/{latitude}/{shopname}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getNearShops1(@PathVariable("longitude") Double longitude
            , @PathVariable("latitude") Double latitude
            , @PathVariable("shopname") String shopname
            , FrontPage frontPage)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxuserService.getNearShops(longitude, latitude, shopname, frontPage.getPagePlus()));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 返回用户附近的商店列表,带有距离数值, *******不带shopcname的首页访问
     * @param longitude
     * @param latitude
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getNearShops/{longitude}/{latitude}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getNearShops(@PathVariable("longitude") Double longitude
            , @PathVariable("latitude") Double latitude
            , FrontPage frontPage)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxuserService.getNearShops(longitude, latitude, null, frontPage.getPagePlus()));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }



    //todo tested
    /**
     * 返回用户创建的商店列表
     * @param userid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getUserShop",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object getUserShop(@RequestHeader("userid") Integer userid)  throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itWxuserService.selectUserShops(userid));
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

}

